package aspectLog;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.junit.After;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by jack on 18/6/2.
 */
@Aspect
@Component
public class AspectLog {
    private Logger logger = LoggerFactory.getLogger(AspectLog.class);

//    @Pointcut("execution(* aspectLog.Singer.sing*(..))")
    @Pointcut("@annotation(aspectLog.LogTag)")
    public void sing_log(){}

    @Before("sing_log()")
    public void doBefore(JoinPoint joinPoint){
        //获取类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取方法名
        String methodName = joinPoint.getSignature().getName();
        //获取方法的签名
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        logger.info("method {} exec ", methodName);
        System.out.println("method_name " + methodName);
    }

    @Pointcut("@annotation(aspectLog.LogTag)")
    public void sing_log_after_returning(){}


    @AfterReturning(pointcut = "sing_log_after_returning()", returning = "res")
    public void doAfter(JoinPoint joinPoint, Object res){
        if (res == null){
            return;
        }

        System.out.println(res);
    }

}
